package com.hngm.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.websocket.Session;

import com.hngm.bean.Company;
import com.hngm.bean.Newssheet;
import com.hngm.bean.Newstype;
import com.hngm.bean.Product;
import com.hngm.bean.Productpicture;
import com.hngm.dao.CompanyDao;
import com.hngm.dao.NewssheetDao;
import com.hngm.dao.NewstypeDao;
import com.hngm.dao.ProductDao;
import com.hngm.dao.ProductpictureDao;
import com.hngm.util.DBManager;

/**
 * Servlet implementation class IndexServlet
 */
public class IndexServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public IndexServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		
		//查询企业信息
		CompanyDao companyDao = new CompanyDao();
		Company company = companyDao.queryCompanyInfo();
		//将企业信息存储的session 中
		HttpSession session = request.getSession();
		session.setAttribute("companyInfo", company);
		
		//查询图片
		ProductpictureDao productpictureDao = new ProductpictureDao();
		ArrayList<Productpicture> ppList = productpictureDao.queryProductpictureList();
		request.setAttribute("ppList", ppList);
		
		//是否上推荐
		ProductDao productDao = new ProductDao();
		List<Product> prList = productDao.queryProductByRec();
		request.setAttribute("prList", prList);
		//是否上轮播
		List<Product> proList = productDao.queryProductByRot();
		session.setAttribute("proList", proList);
		
		//查询新闻内容
		NewssheetDao newssheetDao = new NewssheetDao();
		List<Newssheet> nesList = newssheetDao.queryNewssheetList();
		request.setAttribute("nesList", nesList);
		request.getRequestDispatcher("index.jsp").forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
